package io.renren.modules.news.service.impl;

import io.renren.modules.news.dao.NewsDao;
import io.renren.modules.sys.dto.NewsDto;
import io.renren.modules.news.entity.Comment;
import io.renren.modules.news.entity.News;
import io.renren.modules.news.controller.UserNewsCollection;
import io.renren.modules.news.entity.reply;
import io.renren.modules.news.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class NewsServiceImpl implements NewsService {
    @Autowired
     NewsDao newsDao;

    @Override
    public List getNewsList() {
       List newsList = newsDao.getNewsList();
        for(int i = 0; i <newsList.size();i++){
             News news = (News) newsList.get(i);
            String image = news.getImageList();
            news.setImage(image.split(","));
            news.setImageCount(news.getImage().length+1);
        }
        return newsList;
    }

    @Override
    public News getNews(Long newsId) {
            News news = newsDao.getNews(newsId);
            String image = news.getImageList();
            //news.setImageCount(news.getImage().length+1);
            news.setImage(image.split(","));
        return news;
    }

    @Override
    public void updateNewsReadCount(News n) {
        newsDao.updateNewsReadCount(n);
    }

    @Override
    public List getNewsCommentList(Long newsId) {
         List commentList = newsDao.getNewsCommentList(newsId);
        /*for(int i = 0;i<commentList.size();i++){
            Comment comment = (Comment)commentList.get(i);
            comment.setLikeCount(newsDao.getCommentLikeCount(comment.getCommentId()));
        }*/
        return commentList;
    }

    @Override
    public List getThisNewsAndCommentReply(NewsDto newsDto) {
        return newsDao.getThisNewsAndCommentReply(newsDto);
    }

    @Override
    public List getNewsByType(Map params) {
        List newsList = newsDao.getNewsByType(params);
        for(int i = 0; i <newsList.size();i++){
            News newss = (News) newsList.get(i);
            String image = newss.getImageList();
            if(image != null){
                newss.setImage(image.split(","));
            }else if(image == null && !"".equals(image)){
                String[] images = {};
                newss.setImage(images);
            }
        }
        return newsList;
    }

    @Override
    public List getNewsLike(Map<String,Object> params) {
        List newsList = newsDao.getNewsLike(params);
        for (int i = 0; i < newsList.size(); i++) {
            News newss = (News) newsList.get(i);
            String image = newss.getImageList();
            if (image != null) {
                newss.setImage(image.split(","));
            }else if(image == null && !"".equals(image)){
                String[] images = {};
                newss.setImage(images);
            }
        }
        return newsList;
    }

    @Override
    public void insertComment(Comment comment) {
        newsDao.insertComment(comment);
    }

    @Override
    public void insertReply(reply reply) {
        newsDao.insertReply(reply);
    }

    @Override
    public void addUserNewsCollection(UserNewsCollection userNewsCollection) {
        newsDao.addUserNewsCollection(userNewsCollection);
    }

    @Override
    public void delUserNewsCollection(UserNewsCollection userNewsCollection) {
        newsDao.delUserNewsCollection(userNewsCollection);
    }

    @Override
    public List getUserNewsCollection(Map map) {
        //List newsList = newsDao.getUserNewsCollection(map);
        List newsList = newsDao.getUserNewsCollection(map);
        for (int i = 0; i < newsList.size(); i++) {
            News newss = (News) newsList.get(i);
            String image = newss.getImageList();
            if (image != null) {
                newss.setImage(image.split(","));
            }else if(image == null && !"".equals(image)){
                String[] images = {};
                newss.setImage(images);
            }

        }
        return newsList;
    }

    @Override
    public List checkIsCollectionNews(Map map) {
        return newsDao.checkIsCollectionNews(map);
    }

    @Override
    public long getCommentLikeCount(long commentId) {
        return newsDao.getCommentLikeCount(commentId);
    }

    @Override
    public void saveCommentLike(Map map) {
        newsDao.saveCommentLike(map);
    }

    @Override
    public void updateCommentLike(Map map) {
        newsDao.updateCommentLike(map);
    }

    @Override
    public void delCommentLike(Map map) {
        newsDao.delCommentLike(map);
    }

    @Override
    public void deleteCommentLike(Map map) {
        newsDao.deleteCommentLike(map);
    }

    @Override
    public List checkCommentLike(Map map) {
        return newsDao.checkCommentLike(map);
    }

    @Override
    public List getNewsType() {
        return newsDao.getNewsType();
    }

    @Override
    public List getNewsByTypeNoPage(Map<String, Object> params) {
        Integer count=newsDao.getNewsCount(params);
        String num="";
        Integer limit= Integer.parseInt((String) params.get("limit"));
        for(int i=0;i<limit;i++){
            num= num+   (int)(1+Math.random()*(count-1+1))+",";
        }
        num=num.substring(0,num.length() - 1);
        params.put("num",num );

        List newsList = newsDao.getNewsByTypeNoPage(params);
        for(int i = 0; i <newsList.size();i++){
            News newss = (News) newsList.get(i);
            String image = newss.getImageList();
            if(image != null){
                newss.setImage(image.split(","));
            }else if(image == null && !"".equals(image)){
                String[] images = {};
                newss.setImage(images);
            }
        }
        return newsList;
    }


}
